#!python

import ConfigParser
import glob
import urlparse
import os
import baidu_pcs_op2 as baidu_pcs_op
import shutil
import sys
import traceback
import sqlite3
import time

def init_db():
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    try:
        cur.execute("select count(*) from url")
        cur.fetchall()
    except:
        cur.execute("""create table url(id integer primary key autoincrement, 
        link text,
        name text,
        uptime datetime
        )
       """)
    conn.commit()   
    cur.close()
    conn.close()

def write_links(name):
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    #p = os.path.join(info_root, b + ".txt")
    #fp = open(p)
    #n = ""
    #for line in fp:
    #    if "Origin-Name" in line:
    #        n = line.strip(" \r\n").split(":", 1)[1]
    #        break
    #fp.close()  
    cur.execute("select count(*) from url where name = '%s'" % name)
    if int(cur.fetchone()[0]) > 0:
        cur.execute("update url set link='%s',uptime='%s' where name='%s'" % ( u, t, name))
    else:    
        t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        u = "%s%s" % (url, name)
        cur.execute("insert into url(link, name, uptime) values('%s', '%s', '%s')" % (u, name, t))
    conn.commit()
    cur.close()
    conn.close()

if __name__ == "__main__":
    configfile="server.cfg"
    cfg = ConfigParser.ConfigParser()
    cfg.read(configfile)
    www_root = cfg.get("server","www_root")
    host = cfg.get("server","host")
    port = cfg.get("server","port")
    #url=cfg.get("server","url")
    url = "http://%s:%s/" % (host, port)
    exts = cfg.get("server","exts")
    access_token=cfg.get('server','access_token')
    #pcs_dir = "/apps/WiFiDisk/download"
    pcs_dir = cfg.get("server","save_dir")
    info_root=cfg.get("server","info_root")
    db_name = "links.db"
    init_db()

    extensions = exts.split(",")
    pcs = baidu_pcs_op.PcsOp(access_token)
    try:
        pcs.info(pcs_dir)
    except:
        try:
            pcs.mkdir(pcs_dir)
        except:
            pass
    while True:
        is_empty = 0
        for ext in extensions:
            files = glob.glob(os.path.join(www_root,ext))
            if len(files) == 0:
                print time.asctime(),"no more movie with extension",ext
                is_empty += 1
                continue
            for f in files:
                time.sleep(5)
                bname=os.path.basename(f)
                rname="%s/%s" % (pcs_dir, bname)
                u = urlparse.urljoin(url,bname)

                try:
                    pcs.info(rname)
                    print time.asctime(),bname,"is already downloaded"
                    write_links(bname)
                    try:
                        shutil.move(f, f + "_del")
                    except:
                        traceback.print_exc()
                        print "rename file  failed"
                    # file already downloaded
                    continue
                except:
                    pass

                try:
                    tasks = pcs.list_task()
                except:
                    traceback.print_exc()
                    continue

                task_exists = 0
                for t in tasks:
                    rr = pcs.query_task(t["task_id"])
                    r=rr["task_info"][t["task_id"]]
                    try:
                        print time.asctime(),"%s %.2f%%(%s/%s) %d" % (t["task_name"], float(r["finished_size"])/float(r["file_size"]) * 100,
                            r["finished_size"], r["file_size"],
                            time.time() - int(t["create_time"])
                            )
                    except:
                        traceback.print_exc()
                        
                    #print t["task_name"],time.asctime(time.localtime(int(t["create_time"]))), bname
                    if t["task_name"] == bname:
                        t1=int(t["create_time"])
                        now = time.time()
                        if (now - t1) > (12*60*60): # task over time
                            try:
                                pcs.cancel_task(t["task_id"])
                                print time.asctime(),"cancel task",t["task_name"]
                            except:
                                traceback.print_exc()
                                print "cancel task failed"
                        task_exists = 1
                        #break
                if task_exists:
                    # file is downloading
                    print time.asctime(),bname,"is downloading"
                    continue

                if len(tasks) < 5:
                    try:
                        pcs.add_task(u, pcs_dir)
                    except:
                        traceback.print_exc()
                        print "add task failed",bname
                        continue

                    print time.asctime(),"add",u,"to download queue"
                    continue

                # queue list is full
                # waiting for some task to finish
                print time.asctime(),"waiting some task to finish..."
                while True:
                    time.sleep(20)
                    try:
                        tasks = pcs.list_task()
                    except:
                        traceback.print_exc()
                        continue
                    c = 0    
                    for t in tasks:
                        t1=int(t["create_time"])
                        now = time.time()
                        if (now - t1) > (12*60*60):
                            try:
                                pcs.cancel_task(t["task_id"])
                                print time.asctime(),"cancel task",t["task_name"]
                                c += 1
                            except:
                                traceback.print_exc()
                                print time.asctime(),"cancel task failed"
                    if c:
                        break
                    if len(tasks) < 5:
                        break
                print time.asctime(),"some task is finished"

        if is_empty == len(extensions):
            print time.asctime(),"waiting more movies..."
            time.sleep(300)
        try:
            u,t=pcs.query_space()
            if float(u)/t > 0.8:
                print time.asctime(),"space usage greater than 80%, deleting some files..."
                lsts=pcs.get_filelist()
                pcs.delete_all(lsts)
        except:
            traceback.print_exc()
        time.sleep(30)
